package 二叉树相关问题.中度题;

import java.util.ArrayList;
import java.util.List;

public class 分裂二叉树的最大乘积 {
    List<Long> list = new ArrayList<Long>();
    public int maxProduct(TreeNode root) {
        long num = traverse(root);
        long max=Integer.MIN_VALUE;
        long mod=1000000007;
        for (Long aLong : list) {
            long a=aLong*(num-aLong);
            max=Math.max(max,a);
        }
        return (int)(max%mod);
    }

    private long traverse(TreeNode root) {
        long sum=0;
        if (root==null)
            return 0;
        sum+=root.val+traverse(root.left)+traverse(root.right);
        if (sum!=0)
            list.add(sum);
        return sum;
    }

}
